<?php
/**
 * 文章管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 下午 5:54
 */
namespace app\article\controller\compose;
use app\article\validate\ValiCp;
use app\article\validate\ValidateFun;
use app\common\api\article\ICompose;


class Compose implements ICompose{
    #接收参数
    private $data =[];
    /**
     * 初始化配置
     * Compose constructor.
     */
    public function __construct(array $config=[]){
        $this->data = array_filter_full(array_merge($this->data,$config));
    }

    /**全部文章
     * @return array
     */
    public function cp_list(){
        #参数
        $d_data = [
            'curr'  =>1,
            'limits'=>10
        ];
        $data = array_filter_full(array_merge($d_data,$this->data));

        #获取数据
        $list = new \app\article\model\Compose();
        $res =$list->all_list($data,1);
        #返回数据
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];

    }

    /**
     * 个人发表的文章
     * @return array
     */
    public function cp_info_list(){
        #参数
        $admin = session('admin_info');
        $d_data = [
            'userid'=>$admin['id'],
            'curr'  =>1,
            'limits'=>10
        ];
        $data = array_filter_full(array_merge($d_data,$this->data));

        #获取参数
        $list = new \app\article\model\Compose();
        $res =$list->all_list($data,2);
        #返回数据
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];

    }

    /**
     * 文章类型分类
     */
    public function cp_type(){
        try{
            #参数
            $d_data = [
                'curr'  =>1,
                'limits'=>10
            ];
            $data = array_filter_full(array_merge($d_data,$this->data));

            #验证参数
            $vali = ValiCp::cp_type();
            $validate = new ValidateFun($vali['rule'],$vali['message']);
            if (!$validate->check($data)){
                $err = $validate->getError();
                return ['code'=>$err,'msg'=>config("msg.$err")];
            }
            #获取参数
            $list = new \app\article\model\Compose();
            $res =$list->_type($data);

            #返回数据
            return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];

        }catch ( \Exception $e){
            return ['code'=>201,'msg'=>config('msg.201')];
        }
    }

    /**
     * 按时间分类
     * @return array
     */
    public function cp_time()
    {
        try{
            #参数
            $d_data = [
                'curr'  =>1,
                'limits'=>10
            ];
            $data = array_filter_full(array_merge($d_data,$this->data));
            #验证参数
            $vali = ValiCp::cp_type();
            $validate = new ValidateFun($vali['rule'],$vali['message']);
            if (!$validate->check($data)){
                $err = $validate->getError();
                return ['code'=>$err,'msg'=>config("msg.$err")];
            }
            #获取参数
            $list = new \app\article\model\Compose();
            $res =$list->_time($data);
            #返回数据
            return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];

        }catch ( \Exception $e){
            return ['code'=>201,'msg'=>config('msg.201')];
        }
    }

    /**
     * @return array
     * 某一篇文章
     */
    public function cp_info(){
        #参数
        $admin = session('admin_info');
        $d_data = [
            'userid'=>$admin['id'],
        ];
        $data = array_filter_full(array_merge($d_data,$this->data));

        #获取参数
        $list = new \app\article\model\Compose();
        $res =$list->_list($data);
        #返回数据
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
    }

    /**
     * @return array
     * 添加文章
     */
    public function cp_add(){
        #参数
        $admin = session('admin_info');
        $d_data = [
            'userid'=>$admin['id'],
            'createtime'=>date('Y-m-d H:i:s'),
            'updatetime'=>date('Y-m-d H:i:s'),
            'del'=>1,
        ];
        $data = array_filter_full(array_merge($d_data,$this->data));
        unset($data['id']);

        #验证参数
        $vali = ValiCp::cp_add();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #添加
        $add = new \app\article\model\Compose();
        $res = $add->_add($data);
        #返回
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }
        return ['code'=>10006,'msg'=>config('msg.10006')];

    }

    /**
     * @return array
     * 删除文章
     */
    public function cp_update(){
        #参数
        $data =$this->data;
        #验证参数
        if (!isset($data['del']) && !isset($data['status'])){
            $data['updatetime']=date('Y-m-d H:i:s');
            $vali = ValiCp::cp_update();
            $validate = new ValidateFun($vali['rule'],$vali['message']);
            if (!$validate->check($data)){
                $err = $validate->getError();
                return ['code'=>$err,'msg'=>config("msg.$err")];
            }
        }

        #修改
        $add = new \app\article\model\Compose();
        $res = $add->_update($data);
        #返回
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }
        return ['code'=>10007,'msg'=>config('msg.10007')];

    }
}